<?php
include_once 'admin-functions.php';
include_once '../functions.php';
if (isset($_SESSION['logged_in'])) {} else {header('Location: page.php');}


/* BEGINT THE NORMAL ADD FORM */
echo '<script type="text/javascript">
var xmlHttp
function saveTitle() {
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) {
	alert ("Browser does not support HTTP Request")
	return
	} 
	var url="autosave.php"
	var title = document.getElementById("item_a").value
	var content = document.getElementById("item_c").value

	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("POST",url,true)
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	xmlHttp.send("title="+title+"&content="+content)
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 

		document.getElementById("saveButton").style.backgroundColor = "green"		
} 
} 

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

var secs = 3var timerID = nullvar timerRunning = truevar delay = 1000function InitializeTimer(){    // Set the length of the timer, in seconds    secs = 10    StopTheClock()    StartTheTimer()}function StopTheClock(){    if(timerRunning)        clearTimeout(timerID)    timerRunning = false}function StartTheTimer(){    if (secs==0)    {        StopTheClock()
        saveTitle()
        InitializeTimer()
		StartTheTimer()    }    else    {        self.status = secs        secs = secs - 1        timerRunning = true        timerID = self.setTimeout("StartTheTimer()", delay)    }}

function unsaved() {
			document.getElementById("saveButton").style.backgroundColor = "orange"
}

</script>
';

if (isset($_POST['post']) == 0 && isset($_POST['title']) == 0)  {
	echo "<h2>Add Post</h2><br />";


	echo '<form action="page.php?p=add" method="post">
						<label for="item_a"><strong>Title:</strong></label>
						<p class="input">
							<input onkeydown="unsaved()" name="title" id="item_a" />
						</p>
						<label for="item_c"><strong>Content:</strong></label>
						<p class="text">
							<textarea onkeydown="unsaved()" name="post" id="item_c" rows="20" cols="30"></textarea>
						</p>

						<p class="button">	
							<button name="save" id="saveButton" type="button" onclick="saveTitle()">Save</button>				
							<button name="submit" type="submit">Post</button>							
						</p>			
					</form>
					<div style="margin-top:-50px;">
					<p><strong>Save status:</strong><span id="txtHint"></span></p>
											<p><em>*Post content can be in tipiwiki markup</em></p>
											</div>
';}

/* BEGIN THE ADDING FUNCTION */
elseif (isset($_POST['post']) == 1 && isset($_POST['title']) == 1 && isset($_SESSION['logged_in'])) {
	$title = $_POST['title'];
	$post = $_POST['post'];
	add_post($title, $post, $_SERVER['REQUEST_TIME'], $_SESSION['account_name'], "data.ini");

}


// bring in the grande finale
include_once 'theme/top.php';

?>


